import Vue from 'vue'
import Router from 'vue-router'
import dishesList from '@/pages/dishes/dishesList'
import foodDetails from '@/pages/dishes/components/details'
import shopCartMess from '@/pages/shopCartMess/shopCartMess'
import orders from '@/pages/orders/orders'
import pay from '@/pages/orders/pay'
import apply from '@/pages/dishes/components/apply'
import {store} from '@/store/index.js'

import axios from 'axios'

Vue.use(Router)
const router = new Router({
  //mode: 'history',
  routes: [
    {
      path: '/list',
      name: 'dishesList',
      component: dishesList
    },
    {
      path: '/shopCartMess',
      name: 'shopCartMess',
      component: shopCartMess
    },
    {
      path: '/orders',
      name: 'orders',
      component: orders
    },
    {
      path: '/details',
      name: 'foodDetails',
      component: foodDetails
    },
    {
      path: '/pay',
      name: 'pay',
      component: pay
    },
    {
      path: '/apply',
      name: 'apply',
      component: apply
    },
    //重定向
    {
      path: '/', 
      redirect: '/list' 
    }
  ]
})
function getUrlParmas(name) {
  let reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  let r = window.location.search.substr(1).match(reg);
  if(r!=null)
  return unescape(r[2]);
  return null;
}
let vm = new Vue({
  store
})
// let id = getUrlParmas('id')
// router.beforeEach((to, from, next) => {
//   axios.post('api/listenStatus',{'id':id}).then( res => {
//     if(res.data.code == 200) {
//       if(res.data.data.order_authority) {
//         if(res.data.data.order_authority.order_id){
//           vm.$store.commit('setOrderId',res.data.data.order_authority.order_id)
//         }else {
//           vm.$store.commit('setOrderId','')
//         }
//         if(res.data.data.use_access == 2) {
//           vm.$showModal('您已申请使用该桌号，请耐心等待房桌管理员同意',false,false)
//         }else if(res.data.data.use_access == 4) {
//           vm.$showModal('不好意思，房桌管理员不同意您使用',false,false)
//         }else if(res.data.data.use_access == 5) {
          
//           vm.$showModal('该房桌已被使用，是否申请同桌？',true,false,function() {
//             axios.post('api/scan_eating_in',{'id':id}).then( res => {
//                 console.log(res)
//                 if(res.data.code == 200) {
//                   vm.$router.go(0)
//                 }else {
//                   vm.$toast(res.data.info)
//                 }
                
//               }).catch( error => {
//                 console.log(error)
//               })
//           },function(){},'立即申请')
//         }else if(res.data.data.use_access == 6) {
//           vm.$showModal('该房桌已被占用，请换空桌！',false,false)
//         }else if(res.data.data.use_access == 7) {
          
//           axios.post('api/scan_need_agree',{'id':id}).then( res => {
//             console.log(res)
//             vm.$showModal(res.data.data.user.name+'要申请使用该房桌，是否同意?',true,true,function() {
//                 //同意
//                 axios.post('api/scan_sure_in',{'id':id,'applicant_id':res.data.data.applicant_id,'agree':1}).then( res => {
//                   console.log(res)
//                   //vm.$router.go(0)
//                   vm.$toast(res.data.info)
//                 }).catch( error => {
//                   console.log(error)
//                 })
//               },function() {
//                 //不同意
//                 axios.post('api/scan_sure_in',{'id':id,'applicant_id':res.data.data.applicant_id,'agree':2}).then( res => {
//                   console.log(res)
//                   //vm.$router.go(0)
//                   vm.$toast(res.data.info)
//                 }).catch( error => {
//                   console.log(error)
//                 })
//               },'同意','不同意')
//             }).catch( error => {
//               console.log(error)
//             })
//         }
//       }
//     }else {
//       vm.$showModal(res.data.info,false,false,function() {},function() {})
//     }
    
//     console.log('导航：',res)
//     next()
//   })
// })

export default router
